<template>
  <div class="app-container">
    <el-form :model="form" :rules="rules" label-width="120px" ref="HelpForm">
      <el-form-item label="名称" prop="name" required>
        <el-input v-model="form.name" style="width:500px"/>
      </el-form-item>

      <el-form-item style="margin-bottom: 40px;" label-width="100px" label="广告位:" required>
        <el-select v-model="form.position"  filterable default-first-option remote placeholder="选择广告位">
          <el-option v-for="(item,index) in selectPosition"  :label="item" :value="index" />
        </el-select>
      </el-form-item>

      <el-form-item label="图片" prop="icon" required="">
        <img v-if="form.icon" :src="form.icon" class="avatar" @click="showImageCenter">
        <i v-else @click="showImageCenter" class="el-icon-plus avatar-uploader-icon"></i>
      </el-form-item>

      <el-form-item label="背景色" prop="back_color" v-if="form.position == 'index_top'" >
        <!-- <el-input v-model="form.back_color" style="width:200px"  placeholder="图片背景颜色"/> -->
        <el-color-picker  v-model="form.back_color" show-alpha />
      </el-form-item>

     <el-form-item label="开始时间" prop="timeRange" >
      <el-date-picker v-model="timeRange"  type="datetimerange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" />
     </el-form-item>

      <el-form-item label="排序" prop="sort" required>
        <el-input v-model="form.sort" style="width:200px"/>
      </el-form-item>

      <el-form-item label="状态"  prop="status" required>
        <el-switch
          v-model="form.status"
          active-text="上线"
          inactive-text="下架">
        </el-switch>
      </el-form-item>

      <el-form-item label="跳转链接" prop="href">
        <el-input v-model="form.href" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('HelpForm')" size="small">提 交</el-button>
        <el-button @click="onCancel()" size="small">重置</el-button>
      </el-form-item>

       <ImageCenter :is_multiple=false :Innervisible.sync="dialogImageVisible" :limit="limit"  @change="changeImage" @closeImage="closeImage"/>
    </el-form>
  </div>
  
</template>

<script>
  import {createAdv,updateAdv,detailAdv,getPosition} from '@/api/operations/adv'
  import ImageCenter from '@/components/ImageCenter'
  import { ref } from 'vue'
export default {
  components: {ImageCenter},
  data() {
    return {
      form: {
        name: '',
        icon: '',
        sort:99,
        status:true,
        start_time:'',
        end_time:'',
        href:'',
        position:null
      },
      timeRange:[],
      limit:1, //限制上传数量
      dialogImageVisible:false,
      selectPosition:[],
      cateTree:[],
      default_select:[0],
      rules:{
        name:[{required: true, message: '名称不能为空', trigger: 'blur' }],
        icom:[{required: true, message: '图标不能为空', trigger: 'blur' }],
        //start_time:[{required: true, message: '开始时间不能为空', trigger: 'blur' }],
        //end_time:[{required: true, message: '结束时间不能为空', trigger: 'blur' }],
      }
    }
  },
  beforeMount() {
    let id = this.$route.params.id || 0;
    this.position();
    if(id > 0){
      detailAdv(id).then(response => {
        this.form = response.data
        this.form.status = response.data.status ? true : false
        this.timeRange = [this.form.start_time,this.form.end_time]
      })
    }
  },
  methods: {
    changeImage(res){
      this.dialogImageVisible = false;
      this.form.icon = res[0].url
      //console.log(res)
    },
    showImageCenter() {
      this.dialogImageVisible = true;
      return false;
    },
    closeImage:function(){
      this.dialogImageVisible = false;
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if (valid) {
          this.checkSpec()
          this.form.status = this.form.status ? 1 : 0
          this.form.start_time = this.timeRange[0]
          this.form.end_time = this.timeRange[1]
          let id = this.$route.params.id || 0;

          if(id > 0){
            updateAdv(this.form).then(response => {
              if(response.code == 200){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(()=>{
                  this.$router.push({name:'adv'});
                }, 200)
              }
            })
          }else{
            createAdv(this.form).then(response => {
              if(response.code == 200){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(()=>{
                  this.$router.push({name:'adv'});
                }, 200)
              }
            })
          }
        }
      })
    },
    position(){
        getPosition().then(response=>{
          this.selectPosition = response.data
        })
    },
    checkSpec(){
      let _spec_name = this.form.name
      if (!_spec_name || _spec_name.length<1) {
        this.$message({
          message: '名称不能为空',
          type: 'warning'
        })
        return false;
      }

      let _content = this.form.icon
      if (!_content || _content.length<1) {
        this.$message({
          message: '图标不能为空',
          type: 'warning'
        })
        return false;
      }
      
      return true;
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
.app-container {
  background: #fff;
  margin-top: 18px;
  height: 100%;
  min-height: calc(100vh - 68px);
  font-size: 12px
}

.app-container * {
  font-size: 12px;
}
.avatar,.avatar-uploader-icon {
  width: 90px;
  height: 90px;
  padding: 1px;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 90px;
}
</style>
